/** 
  校际交流院级项目申请--列表
*/
<template>
    <div>
        <div v-if="ifShowList === 'list'">
            <condition-card
                @reset="reset"
                @search="onSubmit"
                label-position="right"
                label-width="150px"
            >
                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="项目开始时间">
                            <el-date-picker
                                v-model="queryForm.condition.projectStartTimeStr"
                                type="date"
                                clearable
                                style="width: 100%"
                                value-format="yyyy-MM-dd"
                                :picker-options="startDatePicker"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="项目结束时间">
                            <el-date-picker
                                v-model="queryForm.condition.projectEndTimeStr"
                                type="date"
                                clearable
                                style="width: 100%"
                                value-format="yyyy-MM-dd"
                                :picker-options="endDatePicker"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="培养层次">
                            <el-select
                                v-model.trim="queryForm.condition.trainingCodes"
                                clearable
                                filterable
                                multiple
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.teachLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="留学内容">
                            <el-input
                                v-model="queryForm.condition.projectStudyAbroadContent"
                                class="topic-input-select"
                                maxlength="32"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核状态">
                            <el-select
                                v-model.trim="queryForm.condition.applyStatus"
                                clearable
                                filterable
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.states"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <!-- 搜索框结束 -->

                <!-- 列表页开始 -->
            </condition-card>
            <el-card shadow="never" v-loading="loading" body-style="padding: 24px;">
                <h1 class="condition-title">校际交流院级项目申请列表</h1>
                <div class="sub-title">
                    <div class="float-right" style="display: flex;">
                        <el-button type="danger" @click="delApp()">删除</el-button>
                        <el-button type="primary" @click="application()">申请</el-button>
                    </div>
                </div>
                <edu-table
                    :pagination="false"
                    :order="true"
                    :tooltipEffect="tooltipEffect"
                    stripe
                    :data="dataList"
                    size="mini"
                    @selection-change="handleSelectionChange"
                >
                    <edu-table-column label="培养层次" prop="trainingName"></edu-table-column>
                    <edu-table-column label="项目名称" prop="projectName"></edu-table-column>
                    <edu-table-column label="项目联系人" prop="projectContactLable" width="110"></edu-table-column>
                    <edu-table-column label="项目负责人" prop="projectHeadLable" width="110"></edu-table-column>
                    <edu-table-column label="国家/地区" prop="projectNationName"></edu-table-column>
                    <edu-table-column label="合作大学" prop="projectCollegeCn"></edu-table-column>
                    <edu-table-column label="项目开始时间" prop="projectStartTime" width="120"></edu-table-column>
                    <edu-table-column label="项目结束时间" prop="projectEndTime" width="120"></edu-table-column>
                    <edu-table-column label="每年的中方留学生名额" prop="projectInlandNumber" width="160"></edu-table-column>
                    <edu-table-column label="每年的外方留学生名额" prop="projectOutlandNumber" width="160"></edu-table-column>
                    <edu-table-column label="留学内容" prop="projectStudyAbroadContent"></edu-table-column>
                    <edu-table-column label="审核状态" prop="applyStatus">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="scope.row.applyStatus !==null"
                                :type="scope.row.applyStatus | getType"
                            >{{scope.row.applyStatus | applyStatus}}</el-tag>
                        </template>
                    </edu-table-column>
                    <edu-table-column label="下一级审批人" prop="approverName" width="120"></edu-table-column>
                    <!-- 操作 -->
                    <edu-table-column :label="$t('base.operate')" fit width="200">
                        <template slot-scope="scope">
                            <el-button type="text" @click="viewApp(scope.row)">查看</el-button>
                            <el-button
                                type="text"
                                v-show="scope.row.applyStatus === 2"
                                @click="cancelProject(scope.row.projectId)"
                            >撤销</el-button>
                            <el-button
                                type="text"
                                v-show="scope.row.applyStatus === 1 || scope.row.applyStatus === 5"
                                @click="editApp(scope.row)"
                            >编辑</el-button>
                            <el-button
                                type="text"
                                v-show="scope.row.applyStatus === 1 || scope.row.applyStatus === 5"
                                @click="delApp(scope.row)"
                            >删除</el-button>
                        </template>
                    </edu-table-column>
                </edu-table>
                <el-row type="flex" class="row-bg" justify="end">
                    <el-col>
                        <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryForm.pageNum_"
                            :page-sizes="[20, 40, 80, 100]"
                            :page-size="queryForm.pageSize_"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="queryForm.total_"
                            style="float:right;margin-top:10px"
                        ></el-pagination>
                    </el-col>
                </el-row>
            </el-card>
        </div>
        <div v-if="ifShowList ==='add'">
            <inter-School-Com-App
                @returnVal="changekey"
                :submitType="submitType"
                :projectId="projectId"
                :pageFrom="pageFrom"
            ></inter-School-Com-App>
        </div>
        <div v-if="ifShowList ==='view'">
            <inter-School-Com-View @returnVal="changekey" :projectId="projectId"></inter-School-Com-View>
        </div>
    </div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary'
import baseUrl from 'common/src/api/internationalCommunication/env'
import UTILS from 'common/src/utils/utils'
import conOfIntAcaConApi from 'common/src/api/internationalCommunication/conOfIntAcaCon'
import InterSchoolComAppApi from 'common/src/api/interSchoolExchange/InterSchoolComApp'
import InterSchoolComApp from './InterSchoolComApp'
import InterSchoolComView from './common/InterSchoolComView'
export default {
    components: {
        'inter-School-Com-App': InterSchoolComApp,
        'inter-School-Com-View': InterSchoolComView
    },
    data() {
        return {
            uploadUrl: `${baseUrl}/pgIntlConference/importConference`,
            startDatePicker: this.beginDate(),
            endDatePicker: this.processDate(),
            submitType: '',
            pageFrom: '',
            projectId: null,
            queryForm: {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    entrance: 1,
                    applyStatus: '',
                    projectStartTimeStr: '',
                    projectEndTimeStr: '',
                    projectStudyAbroadContent: '',
                    trainingCodes: []
                }
            },
            dataList: [],
            ifShowList: 'list',
            multipleSelection: [],
            options: {
                teachLevel: [],
                faculty: [],
                oneSubjectName: [],
                states: [
                    {
                        label: '草稿',
                        value: 1
                    },
                    {
                        label: '已提交',
                        value: 2
                    },
                    {
                        label: '待审核',
                        value: 3
                    },
                    {
                        label: '已通过',
                        value: 4
                    },
                    {
                        label: '已驳回',
                        value: 5
                    }
                ]
            }
        }
    },

    created() {
        // 初始化列表页
        this.getListData()
        this.getTeacherLevel()
    },
    filters: {
        applyStatus(val) {
            switch (val) {
                case 1:
                    return '草稿'
                case 2:
                    return '已提交'
                case 3:
                    return '待审核'
                case 4:
                    return '已通过'
                case 5:
                    return '已驳回'
                default:
                    return ' '
            }
        },
        getType(val) {
            switch (val) {
                case 1:
                    return 'info'
                case 2:
                    return 'info'
                case 3:
                    return 'info'
                case 4:
                    return 'success'
                case 5:
                    return 'warning'
                default:
                    return val
            }
        }
    },
    methods: {
        beginDate() {
            let _this = this
            return {
                disabledDate(time) {
                    if (_this.queryForm.condition.projectEndTimeStr) {
                        //如果结束时间不为空，则小于结束时间
                        return (
                            new Date(_this.queryForm.condition.projectEndTimeStr).getTime() <
                            time.getTime()
                        )
                    } else {
                        // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                }
            }
        },
        processDate() {
            let _this = this
            return {
                disabledDate(time) {
                    if (_this.queryForm.condition.projectStartTimeStr) {
                        //如果开始时间不为空，则结束时间大于开始时间
                        return (
                            new Date(_this.queryForm.condition.projectStartTimeStr).getTime() >
                            time.getTime() + 24 * 60 * 60 * 1000
                        )
                    } else {
                        // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                }
            }
        },

        // 获取培养层次
        getTeacherLevel() {
            InterSchoolComAppApi.queryTrainings().then(res => {
                res.data.forEach(level => {
                    this.options.teachLevel.push({
                        label: level.trainingName,
                        value: level.trainingCode
                    })
                })
            })
        },

        // 申请页返回
        changekey(val) {
            this.ifShowList = val
            this.queryForm.pageSize_ = 20
            this.queryForm.pageNum_ = 1
            this.getListData()
        },
        // 申请
        application() {
            this.ifShowList = 'add'
            this.submitType = 'add'
            this.pageFrom = 'app'
            this.projectId = ''
        },
        // 查看
        viewApp(row) {
            this.ifShowList = 'view'
            this.projectId = row.projectId
        },
        // 编辑页
        editApp(row) {
            this.ifShowList = 'add'
            this.submitType = 'edit'
            this.pageFrom = 'app'
            this.projectId = row.projectId
        },
        // 删除
        delApp(row) {
            let _this = this
            let parm = []

            if (row) {
                this.multipleSelection = []
                this.multipleSelection.push({
                    id: row.projectId,
                    applyStatus: row.applyStatus
                })
            }
            if (this.multipleSelection.length === 0) {
                _this.$message.error('请勾选数据')
            } else if (_this.multipleSelection.length > 0) {
                for (let i = 0; i < _this.multipleSelection.length; i++) {
                    if (
                        _this.multipleSelection[i].applyStatus !== 1 &&
                        _this.multipleSelection[i].applyStatus !== 5
                    ) {
                        _this.$message.error('只有草稿和驳回状态数据可删除')
                        return false
                    }
                }

                this.multipleSelection.forEach(proId => {
                    parm.push(proId.id)
                })

                this.$delConfirm().then(_ => {
                    InterSchoolComAppApi.remove(parm).then(res => {
                        if (res.code === 200) {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            })
                            _this.queryForm.pageNum_ = 1
                            _this.queryForm.pageSize_ = 20
                            _this.getListData()
                        }
                    })
                })
            }
        },
        // 撤销院级项目
        cancelProject(val) {
            let parm = {
                projectId: val
            }

            this.$confirm('是否撤销?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                InterSchoolComAppApi.cancel(parm).then(res => {
                    if (res.code === 200) {
                        this.$message({
                            message: '撤销成功',
                            type: 'success'
                        })
                        this.queryForm.pageNum_ = 1
                        this.queryForm.pageSize_ = 20
                        this.getListData()
                    }
                })
            })
        },
        test() {},
        // 重置筛选框
        reset() {
            this.queryForm = {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    entrance: 1,
                    applyStatus: '',
                    projectStartTimeStr: '',
                    projectEndTimeStr: '',
                    projectStudyAbroadContent: '',
                    trainingCodes: []
                }
            }
            this.getListData()
        },

        // 查询按钮
        onSubmit() {
            this.queryForm.pageNum_ = 1
            this.queryForm.total_ = 0
            this.queryForm.pageSize_ = 20
            this.getListData()
        },

        // 获取列表
        getListData() {
            let _this = this
            this.dataList = []
            this.multipleSelection = []
            InterSchoolComAppApi.list(this.queryForm).then(res => {
                res.data.list.forEach(revList => {
                    revList.trainings.forEach(tra => {
                        if (revList.trainingName) {
                            revList.trainingName = revList.trainingName + ',' + tra.trainingName
                        } else {
                            revList.trainingName = tra.trainingName
                        }
                    })
                    _this.dataList.push(revList)
                })

                _this.queryForm.pageSize_ = res.data.pageSize_
                _this.queryForm.total_ = res.data.total_
                _this.queryForm.pageNum_ = res.data.pageNum_
            })
        },
        handleSelectionChange(val) {
            this.multipleSelection = []
            val.forEach(menu => {
                this.multipleSelection.push({ id: menu.projectId, applyStatus: menu.applyStatus })
            })
        },
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val
            this.getListData()
        },
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val
            this.getListData()
        }
    }
}
</script>
<style lang="scss" scoped>
.condition-title {
    color: #2b3b4e;
    font-size: 18px;
    line-height: 24px;
    font-weight: bold;
}
.sub-title {
    margin: 16px 0 65px 0;
    font-size: 16px;
    color: #000020;
    letter-spacing: 0;
}
</style>
